// pages/wholeHouse/list/list.js
const app = getApp()
var leftHeight = 0
var rightHeight = 0 //分别定义左右两边的高度
var query = null
Page({

  /**
   * 页面的初始数据
   */
  data: {
    stepActive: 3, //步骤激活下标

    // 瀑布流
    sysMomentList:[],
    // 瀑布流
    list: [],
    leftList: [],
    rightList: [],
  },
  // 瀑布流
  async isLeft() {
    const {
      list,
      leftList,
      rightList
    } = this.data;
    query = wx.createSelectorQuery();
    for (const item of list) {
      leftHeight <= rightHeight ? leftList.push(item) : rightList.push(item); //判断两边高度，来觉得添加到那边
      await this.getBoxHeight(leftList, rightList);
    }
  },
  getBoxHeight(leftList, rightList) { //获取左右两边高度
    return new Promise((resolve, reject) => {
      this.setData({
        leftList,
        rightList
      }, () => {
        query.select('#left').boundingClientRect();
        query.select('#right').boundingClientRect();
        query.exec((res) => {
          leftHeight = res[0].height; //获取左边列表的高度
          rightHeight = res[1].height; //获取右边列表的高度
          resolve();
        });
      });
    })
  },
  navigateToDetail(e) {
    const momentId = e.currentTarget.dataset.momentid //跳转参数
    const path = e.currentTarget.dataset.path
    // console.log(path + "?momentId=" + momentId);

    wx.navigateTo({
      url: `${path}?momentId=${momentId}`
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this
    wx.showLoading({
      title: '加载中...',
      success: () => {}
    })
    // console.log(options);
    var data = JSON.parse(options.data)
    console.log(data);

    // /mobileUser/decoration/list    获取推荐列
    var postObj = {
      url: '/mobileUser/decoration/list',
      method: 'post',
      data: data
    }
    app.request.loginRequest(postObj).then(res => {
      wx.hideLoading({
        complete: (res) => {},
      })
      if (res.data.code === 200) {

        var data = res.data.data[0].sysMomentList
        console.log('获取推荐列', data)
        that.setData({
          sysMomentList: data
        })

        if (data.length > 0) {
          data.forEach((items, i) => {
            items.sysUser.avatar = app.util.config().api + items.sysUser.avatar
            if (!!items.sysMomentFile&&items.sysMomentFile.length > 0) {
              items.sysMomentFile.forEach((items1, i) => {
                items1.url = app.util.config().api + items1.url
              })
            }
          })
        }
        var list = [...that.data.list, ...data]

        that.setData({
          list: list
        })
        // console.log('获取瞬间列表', this.data.list);

        if (data.length > 0) {
          that.isLeft()
        }
      }
    })



    this.isLeft();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})